import * as Router from 'koa-router'
import * as Joi from 'joi'
import * as bookService from '../services/books.service'
import * as userService from '../services/user.service'
import { badParams, stats } from '../stats'

const router = new Router({
  prefix: '/api/book'
})

// 添加教材
router.post('/add', async ctx => {
  const user = await ctx.state.userInfo
  const schema = Joi.object({
    title: Joi.string().min(2).max(20).required(),
    banner: Joi.array().items(Joi.string().required()).min(1).required(),
    major: Joi.array().items(Joi.string().required()).min(1).required(),
    content: Joi.string().required(),
    books: Joi.array()
      .items(
        Joi.object().keys({
          bookname: Joi.string().min(2).max(8).required(),
          price: Joi.number().min(5).required(),
          stock: Joi.number().min(1).required()
        })
      )
      .min(1)
      .required()
    // tags: Joi.array().items(Joi.string().required().allow('')).min(1).required()
  })
  const { value, error } = schema.validate(ctx.request.body)

  if (error) throw badParams(error.message)
  if (!user) throw stats.ERR_NOT_LOGIN
  if (user) {
    await bookService.issuebook(user._id, value)
    ctx.body = {
      stat: 'OK',
      msg: '书籍发布成功'
    }
  }
})

// 更新书籍
router.post('/update', async ctx => {
  const user = await ctx.state.userInfo
  const schema = Joi.object({
    bookId: Joi.string().required(),
    title: Joi.string().min(2).max(20).required(),
    banner: Joi.array().items(Joi.string().required()).min(1).required(),
    major: Joi.array().items(Joi.string().required()).min(1).required(),
    books: Joi.array()
      .items(
        Joi.object().keys({
          bookname: Joi.string().min(2).max(8).required(),
          price: Joi.number().min(5).required(),
          stock: Joi.number().min(1).required()
        })
      )
      .min(1)
      .required(),
    content: Joi.string().required()
    // tags: Joi.array().items(Joi.string().required().allow('')).min(1).required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  if (user) {
    const res = await bookService.updatebook(value.bookId, value)
    if (res) {
      ctx.body = {
        stat: 'OK',
        msg: '书籍修改成功'
      }
    }
  }
})

// 删除书籍 ok
router.post('/del', async ctx => {
  const user = await ctx.state.userInfo
  const schema = Joi.object({
    bookId: Joi.string().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const res = await bookService.delbook(value.bookId, user._id)
  if (res) {
    ctx.body = {
      stat: 'OK',
      msg: '删除文章成功'
    }
  }
})

// 收藏点赞书籍
router.post('/deal/colathu', async ctx => {
  const user = await ctx.state.userInfo
  if (!user) throw stats.ERR_NOT_LOGIN
  const schema = Joi.object({
    bookId: Joi.string().required(),
    type: Joi.number().required(),
    flag: Joi.boolean().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const msg = await bookService.dealBook(value, user._id)
  ctx.body = {
    stat: 'OK',
    msg
  }
})

// 书籍列表主页
router.post('/list/index', async ctx => {
  const schema = Joi.object({
    skip: Joi.number().required(),
    limit: Joi.number().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const { list, total } = await bookService.blist(value)
  ctx.body = {
    stat: 'OK',
    data: { list, total }
  }
})

// 书籍列表个人页
router.post('/list/mine', async ctx => {
  const schema = Joi.object({
    destId: Joi.string().required(),
    type: Joi.number().required(), // 收藏, 订单,商品之类的
    skip: Joi.number().required(),
    limit: Joi.number().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const data = await bookService.personlist(value)
  ctx.body = {
    stat: 'OK',
    data
  }
})

// 搜索筛选列表
router.post('/list/search', async ctx => {
  const schema = Joi.object({
    skip: Joi.number().required(),
    limit: Joi.number().required(),
    filterTime: Joi.boolean().required(),
    text: Joi.string(),
    major: Joi.array().items(Joi.string().required()).min(1)
    // tag: Joi.string(),
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const data = await bookService.searchlist(value)
  ctx.body = {
    stat: 'OK',
    data
  }
})

// 书籍详情页
router.post('/detail', async ctx => {
  const token = ctx.cookies.get('token')
  const schema = Joi.object({
    bookId: Joi.string().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  if (token) {
    const user = await userService.userInfo(token)
    const res = await bookService.bookinfo(value.bookId, user._id)
    ctx.body = {
      stat: 'OK',
      data: res
    }
  } else {
    const res = await bookService.bookinfo(value.bookId)
    ctx.body = {
      stat: 'OK',
      data: res
    }
  }
})

// 加入购物车 flag ture是添加 false 是删除
router.post('/deal/car', async ctx => {
  const schema = Joi.object({
    bookId: Joi.string().required(),
    indexs: Joi.array().items(Joi.number().required()).min(1),
    flag: Joi.boolean().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const user = await ctx.state.userInfo
  if (user) {
    const msg = await bookService.dealCar(value, user._id)
    ctx.body = {
      stat: 'OK',
      msg
    }
  } else throw stats.ERR_NOT_LOGIN
})

// 购物车列表
router.get('/list/car', async ctx => {
  const user = await ctx.state.userInfo
  if (user) {
    const { list, total } = await bookService.carlist(user._id)
    ctx.body = {
      stat: 'OK',
      data: { list, total }
    }
  } else throw stats.ERR_NOT_LOGIN
})

export default router
